<?php
namespace Common\Mongo;

class SiteMongo extends CommonMongo {
    /**
     * 通过网站ID获取网站信息
     *
     * @param     int   $sid
     * @return    void
     **/
    public function get($sid) {
        $sid = (string)$sid;
        return $this->where(array("sid"=>$sid))->find();   
    }
    /**
     * 添加网站缓存
     *
     * @param     
     * @return    void
     **/
    public function addSite($sid) {
        $sid = (string)$sid;

        $data = $this->where(array('sid'=>$sid))->find();
        if($data) E('网站缓存已经存在');

        $data = M('Site')->field('id as sid,title,domain as host,tid,logo,record,statistic,keywords,description,status')->find($sid);
        if(!$data) E('网站不存在');

        $result = $this->add($data);

        return $result;
    }
    /**
     * 更新网站缓存
     *
     * $type: site、category、tags、menu、template、advertise、link
     * @param     void
     * @return    void
     **/
    public function updateSite($sid,$type = null) {
        $sid = (string)$sid;

        $data = $this->where(array('sid'=>$sid))->find();
        if(!$data) E('网站缓存不存在');
        unset($data['_id']);

        // 网站基础信息
        if($type === null || $type === 'site') {
            $tmp = M('Site')->field('title,domain as host,tid,logo,record,statistic,keywords,description,status')->find($sid);
            foreach($tmp as $key=>$value) {
                $data[$key] = $value;
            }
        }
        // 分类信息
        if($type === null || $type === 'category') {
            $categoryList = M('Category')->where("`sid`={$sid} and `status`=1")->order('sort desc')->field('id,title,auto_publish')->select();
            $tmp = array();
            foreach($categoryList as $value ) {
                $tmp[$value['id']] = $value;
            }
            $data['category'] = $tmp;
        }
        // 菜单信息
        if($type === null || $type === 'menu') {
            $menuList = M('Menu')->where("`sid`={$sid} and `status`=1")->order('sort desc')->field('id,title,type,value')->select();
            $tmp = array();
            foreach($menuList as $value ) {
                $tmp[$value['id']] = $value;
            }
            $data['menu'] = $tmp;
        }
        // 广告信息
        if($type === null || $type === 'advertise') {
            $advertiseList = M('Advertise')->where("`sid`={$sid} and `status`=1 and (`expiretime` > ".NOW_TIME." or `expiretime` = 0)")->field('code,html,expiretime')->select();
            $tmp = array();
            foreach($advertiseList as $value ) {
                $tmp[$value['code']] = $value;
            }
            $data['advertise'] = $tmp; 
        }
        // 友情链接信息
        if($type === null || $type === 'link') {
            $linkList = M('Link')->where("`sid`={$sid} and `status`=1")->order('sort desc')->field('id,title,url')->select();
            $tmp = array();
            foreach($linkList as $value ) {
                $tmp[$value['id']] = $value;
            }
            $data['link'] = $tmp;
        }
        $result = $this->where(array('sid'=>$sid))->save($data);
        if(!$result || !$result['updatedExisting']) {
            E('网站缓存更新失败');
        }
    }

    /**
     * 配置模板
     *
     * @param     void
     * @return    void
     **/
    public function setTemplate($sid,$data) {
        v($sid,'notEmpty','还没选择网站');
        v($sid,'isNumber','非法网站ID');

        $siteInfo = M('Site')->find($sid);
        v($siteInfo,'notEmpty','网站不存在');
        v($siteInfo['tid'],'notEmpty','请先选择模板');

        $templateInfo = M('Template')->find($siteInfo['tid']);
        v($templateInfo,'notEmpty','模板不存在');

        $config = json_decode($templateInfo['config'],true);

        foreach($config as $name=>$value) {
            $type = $value['type'];
            if($type === 'article') {
                $info = (!isset($data[$name]) || !$data[$name]) ? '' : $data[$name];
            }elseif($type === 'tags') {
                $info = (!isset($data[$name]) || !$data[$name]) ? '' : $data[$name];
            }elseif ($type === 'textarea') {
                $info = (!isset($data[$name]) || !$data[$name]) ? '' : $data[$name];
            }elseif ($type === 'slider') {
                $info  = array();
                for($i=1;$i<=$value['maxnum'];$i++) {
                    try{
                        if(isset($_FILES[$name]['tmp_name'][$i]) && $_FILES[$name]['tmp_name'][$i]) {
                            $imageInfo = uploadImage($name,'template',$i);
                            $imagepath = $imageInfo['path'];
                        }elseif (isset($data[$name][$i]['image']) && $data[$name][$i]['image']) {
                            $imagepath = $data[$name][$i]['image'];
                        }else {
                            continue;
                        }
                        $info[$i] = array(
                            'image' =>  $imagepath,
                            'text'  =>  isset($data[$name][$i]['text']) ? $data[$name][$i]['text'] : '',
                            'url'   =>  isset($data[$name][$i]['url']) ? $data[$name][$i]['url'] : '',
                        );
                    }catch(\Exception $e) {
                        continue;
                    }
                }
            }elseif ($type === 'image') {
                $info  = array();
                try{
                    if(isset($_FILES[$name]['tmp_name']) && $_FILES[$name]['tmp_name']) {
                        $imageInfo = uploadImage($name,'template');
                        $imagepath = $imageInfo['path'];
                    }elseif (isset($data[$name]['image']) && $data[$name]['image']) {
                        $imagepath = $data[$name]['image'];
                    }else {
                        continue;
                    }
                    $info = array(
                        'image' =>  $imagepath,
                        'text'  =>  isset($data[$name]['text']) ? $data[$name]['text'] : '',
                        'ext'   =>  isset($data[$name]['ext']) ? $data[$name]['ext'] : '',
                    );
                }catch(\Exception $e) {
                    continue;
                }
            }
            $config[$name] = $info;
        }

        $result = $this->where(array('sid'=>$siteInfo['id']))->save(array(
            'sid'       =>  $sid,
            'tid'       =>  $siteInfo['tid'],
            'template'  =>  $config
        ));
        return $result;
    }


}
?>
